<template>
	<view class="container">
	    <view class="form">
	      <input class="input" v-model="form.accountNumber" placeholder="请输入账号" />
	      <input class="input" v-model="form.password" placeholder="请输入密码" type="password" />
	      <button class="button" @click="handleLogin">登录</button>
	      <view class="link" @click="goToRegister">没有账号？去注册</view>
	    </view>
		<view class="wxBtn" @click="wxLogin">
		    微信登录
		</view>
	</view>

</template>

<script setup>
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	
	import { useUserInfoStore } from '../../store';

	//获得store实例对象
	const userInfoStore = useUserInfoStore()
	
	// 定义响应式状态
	const form = ref({
	  accountNumber: '',
	  password: '',
	});	
	
	// 页面加载时执行
	onLoad(() => {
	  console.log('登录页面加载');
	  
	  form.value.accountNumber = userInfoStore.userInfo.accountNumber;
	  form.value.password = userInfoStore.userInfo.password;
	  if(form.value.accountNumber === "") return
	  handleLogin() 
	  
	  
	});

	// 登录逻辑
	const handleLogin = async () => {
	  if (form.value.accountNumber && form.value.password) {
		  
		  // let regex = /(?<=\.{1})[^\.]+?(?=[,，；]|$)/g;
		  // let match = item.meaning.match(regex);
		uni.showLoading({
			title: "正在登陆",
			mask: true
		})
		
		// try {
			const res = await uniCloud.callFunction({
			  name: 'login',
			  data: {
				accountNumber: form.value.accountNumber,
				password: form.value.password
			  }
			})
		// } catch(e) {
		// 	uni.showToast({
		// 		title: e.message,
		// 		icon: 'none'
		// 	})
		// 	return;
		// }

		
		console.log(res)
		if (res.result.code === 200) {
		  uni.showToast({
			title: '登录成功',
			icon: 'success'
		  })
		  userInfoStore.setUserInfo(res.result.data)
		  // 登录成功后跳转到首页或其他页面
		  uni.switchTab({
			url: '/pages/tabs/progress/progress'
		  })
		  uni.hideLoading()
		} else {
		  uni.showToast({
			title: res.result.message,
			icon: 'none'
		  })
		}
		
	  } else {
	    uni.showToast({
	      title: '请输入用户名和密码',
	      icon: 'none',
	    });
	  }
	};
	
	// 跳转到注册页面
	const goToRegister = () => {
	  uni.navigateTo({
	    url: '/pages/register/register',
	  });
	};
	

	
	function wxLogin() {
		uni.showLoading({
			title: "正在登陆",
			mask: true
		})
	    uni.getUserInfo({
	        provider: 'weixin',
	        desc: "登录的数据",
	        success(user) {
	            user.authResult
	            uni.login({
	                provider: 'weixin',
	                onlyAuthorize: true,
	                success(lres) {
	                    console.log(lres, "请求微信登录返回的数据")
	
	                    if (lres.code) {
	                        uniCloud.callFunction({
	                            name: "wxLogin",
	                            data: {
	                                code: lres.code
	                            }
	                        }).then(res => {
								if(res.result.code === 0){
									uni.showToast({
									  title: '该微信未绑定',
									  icon: 'error',
									});
									return;
								}
								if (res.result.code === 200) {
									console.log("登录/注册成功 ====> ", res)
									uni.showToast({
										title: '登录成功',
										icon: 'success'
									})
									userInfoStore.setUserInfo(res.result.data)
									// 登录成功后跳转到首页或其他页面
									uni.switchTab({
										url: '/pages/tabs/progress/progress'
									})
									
								} else {
									uni.showToast({
									  title: res.result.message,
									  icon: 'none'
									})
								}
								uni.hideLoading()
							})
							// .catch((e) => {
							// 	uni.showToast({
							// 		title: e.message,
							// 		icon: 'none'
							// 	})
							// 	return;
							// })
	                    }
	                }
	            })
	        }
	    })
	}
	
    // function wxLogin() {
    //     uni.getUserInfo({
    //         provider: 'weixin',
    //         desc: "登录的数据",
    //         success(user) {
    //             user.authResult
    //             uni.login({
    //                 provider: 'weixin',
    //                 onlyAuthorize: true,
    //                 success(lres) {
    //                     console.log(lres, "请求微信登录返回的数据")

    //                     if (lres.code) {
    //                         uniCloud.callFunction({
    //                             name: "wxLoginAndRegister",
    //                             data: {
    //                                 code: lres.code
    //                             }
    //                         }).then(res => {
    //                             console.log("登录/注册成功 ====> ", res)
								
				// 				userInfoStore.setUserInfo(res.result)
				// 				// 登录成功后跳转到首页或其他页面
				// 				uni.switchTab({
				// 					url: '/pages/tabs/progress/progress'
				// 				})
				// 			})
    //                     }
    //                 }
    //             })
    //         }
    //     })
    // }
</script>

<style lang="scss" scoped>
	.container {
	  display: flex;
	  flex-direction: column;
	  justify-content: center;
	  align-items: center;
	  padding-top: 200rpx;
	  gap: 100rpx;
	  // height: 100vh;
	  .form {
	    width: 80%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		.input {
		  width: 100%;
		  height: 40px;
		  margin-bottom: 20px;
		  padding: 10px;
		  border: 1px solid #ccc;
		  border-radius: 5px;
		}
		
		.button {
		  width: 100%;
		  height: 40px;
		  background-color: #007aff;
		  color: #fff;
		  border-radius: 5px;
		  display: flex;
		  justify-content: center;
		  align-items: center;
		}
		
		.link {
		  margin-top: 10px;
		  color: #007aff;
		  text-align: center;
		  text-decoration: underline;
		}
	  }
	  
	  .wxBtn{
		  width: 80%;
		  padding: 20rpx 0;
		  background-color: #18BB7C;
		  color: #fff;
		  border-radius: 5px;
		  display: flex;
		  justify-content: center;
		  align-items: center;
		  font-size: 50rpx;
	  }
	}




</style>